package com.zhixinhuixue.zsyte.xxx.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import androidx.core.view.GravityCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.google.gson.Gson;
import com.zhixinhuixue.zsyte.xxx.R;
import com.zhixinhuixue.zsyte.xxx.app.base.BaseActivity;
import com.zhixinhuixue.zsyte.xxx.app.bean.Contanst;
import com.zhixinhuixue.zsyte.xxx.app.bean.MenuResponse;
import com.zhixinhuixue.zsyte.xxx.app.util.StatusBarUtil;
import com.zhixinhuixue.zsyte.xxx.databinding.ActivityHomeBinding;
import com.zhixinhuixue.zsyte.xxx.ui.adapter.MenuAdapter;
import com.zhixinhuixue.zsyte.xxx.ui.viewmodel.HomeViewModle;

import java.util.List;

import me.hgj.mvvmhelper.base.BaseViewModel;

public class HomeActivity extends BaseActivity<HomeViewModle, ActivityHomeBinding> {

    private RecyclerView rvMenu;//列表

    @SuppressLint("ResourceAsColor")
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

        StatusBarUtil.setColor(HomeActivity.this, getResources().getColor(R.color.colorBlack), 0);
        StatusBarUtil.setDarkMode(HomeActivity.this);
       mToolbar.setCenterTitle("");
       mToolbar.setVisibility(View.GONE);
        mDataBind.toolbar.setLeftBackgroundColor(R.mipmap.menumap);
        //工具栏按钮点击
        mDataBind.toolbar.setOnLeftClickListener(v -> mDataBind.drawerLayout.openDrawer(GravityCompat.START));
        //获取头部视图
        View headerView = mDataBind.navView.getHeaderView(0);
        //头像点击
        headerView.findViewById(R.id.iv_avatar).setOnClickListener(v -> showMsg("头像"));

        //绑定列表控件
        rvMenu = headerView.findViewById(R.id.rv_menu);
        //显示菜单列表
        showMenuList();
    }
    /**
     * Toast提示
     *
     * @param msg 内容
     */
    private void showMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
    /**
     * 显示菜单列表
     */
    private void showMenuList() {
        //解析JSON数据
        MenuResponse menuResponse = new Gson().fromJson(Contanst.JSON, MenuResponse.class);
        if (menuResponse.getCode() == Contanst.SUCCESS) {
            //为空处理
            if(menuResponse.getData() ==null){
                showMsg("返回菜单数据为空");
                return;
            }
            List<MenuResponse.DataBean> data = menuResponse.getData();
            //设置适配器的布局和数据源
            MenuAdapter menuAdapter = new MenuAdapter(R.layout.item_menu, data);
            //item点击事件
            menuAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                showMsg(data.get(position).getName());
                //关闭滑动菜单
                mDataBind.drawerLayout.closeDrawer(GravityCompat.START);
            });
            //设置线性布局管理器，默认是纵向
            rvMenu.setLayoutManager(new LinearLayoutManager(this));
            //设置适配器
            rvMenu.setAdapter(menuAdapter);
            //添加item的分隔线
            rvMenu.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        } else {
            //错误提示
            showMsg(menuResponse.getMsg());
        }
    }
}
